import { getToken, setToken, removeToken } from '@/utils/auth'
import { login, getUserInfo } from '@/api/user'
const state = {
  token: getToken() || '',
  userInfo: {}// 存储用户信息
}
const mutations = {
  // 存储token
  setToken(state, newToken) {
    state.token = newToken
    // 持久化
    setToken(newToken)
  },
  // 清除token
  removeToken(state) {
    state.token = ''
    removeToken()
  },
  updatInfo(state, info) {
    state.userInfo = info
  }
}
const actions = {
// 发送请求实现登陆
  async login(context, data) {
    console.log(data, 'data')
    const token = await login(data)
    // console.log(res, '123')
    context.commit('setToken', token)
  },
  async getInfo(context) {
    const info = await getUserInfo()
    // console.log(info)
    context.commit('updatInfo', info)
  },
  loginout(context) {
    // 清除vuex中toekn
    context.commit('removeToken')
    // 清除vuex中用户信息
    context.commit('updatInfo', {})
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}

